package com.woniuxy.repay.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.woniuxy.entity.RepaymentManage;
import com.woniuxy.entity.RepaymentRegistration;
import com.woniuxy.repay.mapper.RepaymentManageMapper;
import com.woniuxy.repay.mapper.RepaymentRegistrationMapper;
import org.springframework.cache.annotation.CachePut;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author shichen
 * @create 2021-11-15-15:50
 * @description 每个月判断贷款状态 ，每个月10号凌晨两点判断状态
 */
@Component
@EnableAsync
@EnableScheduling
public class Calculate {

    private static final Log log = LogFactory.get();

    @Resource
    RepaymentRegistrationMapper repaymentRegistrationMapper;

    @Resource
    RepaymentManageMapper repaymentManageMapper;

    /**
     *
     * @return
     */
    @Async
    @CachePut
    @Scheduled(cron = "0 0 2 10 * ? ")
    public List amethod(){

        log.debug("开始每月结算");
        DateTime date = DateUtil.date();
        date = DateUtil.beginOfDay(date);
        DateTime newDate = DateUtil.offsetMonth(date, 1);

        // 先查上个周期的还款情况 10号凌晨是最后还款日所以直接查这个月的凌晨

        RepaymentRegistration repaymentRegistration =
                new RepaymentRegistration().setRepaymentDate(date);
        List<RepaymentRegistration> list = repaymentRegistrationMapper.selectByCondition(repaymentRegistration);

        for (RepaymentRegistration registration : list) {

            log.debug("select result : {}", registration);

            BigDecimal totalRepayment = registration.getTotalRepayment();
            BigDecimal actualRepaymentAmount = registration.getActualRepaymentAmount();

            // 如果抛异常就说明有问题，就是比较的对象为空，就是钱没还完
            try {
                int i = totalRepayment.compareTo(actualRepaymentAmount);
                log.debug("结果：{}",i);

                // 如果不相等就是钱没还完 ， 也是逾期
                if(i != 0){
                    repaymentManageMapper.updateByCondition(new RepaymentManage()
                            .setId(registration.getManageInfo()).setPaymentStatus("2"));
                    // 只是只还了一部分，后面业务可以拓展
                }
                // 换完了钱
                if (i==0){
                    // 状态为已还款
                    repaymentManageMapper.updateByCondition(new RepaymentManage()
                            .setId(registration.getManageInfo()).setPaymentStatus("1"));
                }
            }catch (NullPointerException e){
                    // 一点都换 ，改表的状态的为  逾期
                    // 设置催收表
                repaymentManageMapper.updateByCondition(new RepaymentManage()
                        .setId(registration.getManageInfo()).setPaymentStatus("2"));
            }

            // 查询合同表看是否 还 完钱
            repaymentRegistrationMapper.insertByCondition(
                    new RepaymentRegistration().setRepaymentDate(newDate)
                            .setManageInfo(repaymentRegistration.getManageInfo())
                            .setTotalRepayment(repaymentRegistration.getTotalRepayment())
                            .setInterestRepayable(repaymentRegistration.getInterestRepayable())
                            .setPrincipalRepayable(repaymentRegistration.getPrincipalRepayable()));

        }
        // 开始新建下一轮的还款管理表

        // 根据合同查询还没有还完钱的用户
        for (RepaymentRegistration registration : list) {
            repaymentRegistrationMapper.insertByCondition(new RepaymentRegistration());
        }


        return null;
    }

}
